from django.db import models
from django.contrib.auth.models import User


# Create your models here.


class BOOK(models.Model):
    name = models.CharField(verbose_name='书名', max_length=32)
    price = models.PositiveSmallIntegerField(verbose_name='价格')
    publish = models.ForeignKey('Publish', on_delete=models.CASCADE)
    authors = models.ManyToManyField('Author')
    date = models.DateField(auto_now_add=True)

    def __str__(self):
        return self.name


class Author(models.Model):
    name = models.CharField(verbose_name='姓名', max_length=32)
    sex_choice = ((1, 'male'),
                  (2, 'female'))
    sex = models.SmallIntegerField(choices=sex_choice, default=1)

    def __str__(self):
        return self.name


class Publish(models.Model):
    name = models.CharField(verbose_name='出版社名称', max_length=32)
    place = models.CharField(verbose_name='地点', max_length=32)

    def __str__(self):
        return self.name


class UserInfo(models.Model):
    """账号表"""
    user = models.OneToOneField(User, on_delete=models.CASCADE)
    name = models.CharField(max_length=32, verbose_name='姓名')
    year = models.PositiveSmallIntegerField(verbose_name='年龄')

    def __str__(self):
        return self.name
